/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.service.impl;

import com.workcell.workdata.im.component.event.GroupEvent;
import com.workcell.workdata.im.component.holder.UidHolder;
import com.workcell.workdata.im.component.redis.GroupRedisTemplate;
import com.workcell.workdata.im.constant.Common;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.entity.EventMessage;
import com.workcell.workdata.im.entity.GroupNotice;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.model.vo.NoticeVO;
import com.workcell.workdata.im.repository.GroupNoticeRepository;
import com.workcell.workdata.im.repository.GroupRepository;
import com.workcell.workdata.im.service.GroupNoticeService;
import com.workcell.workdata.im.util.PageX;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class GroupNoticeServiceImpl implements GroupNoticeService {

	@Resource
	private GroupNoticeRepository groupNoticeRepository;

	@Resource
	private GroupRedisTemplate groupRedisTemplate;

	@Resource
	private GroupRepository groupRepository;

	@Resource
	private ApplicationContext applicationContext;


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(long groupId, String notice) {


		groupRepository.updateNotice(groupId,notice);
		groupRedisTemplate.remove(groupId);

		long uid = UidHolder.getUid();

		GroupNotice groupNotice = new GroupNotice();
		groupNotice.setGroupId(groupId);
		groupNotice.setText(notice);
		groupNotice.setUid(uid);
		groupNotice.setCreateTime(new Date());
		groupNoticeRepository.saveAndFlush(groupNotice);

		Message message = new EventMessage();
		message.setSender(groupId);
		message.setAction(MessageAction.ACTION_305);
		message.setContent(notice);

		GroupEvent groupEvent = new GroupEvent(message, uid);
		groupEvent.addIgnored(uid);

		applicationContext.publishEvent(groupEvent);

	}

	@Override
	public Page<NoticeVO> queryPage(long groupId, int currentPage) {
		Pageable pageable = PageRequest.of(currentPage, Common.API_PAGE_SIZE, Sort.by(Sort.Order.desc("id")));
		Page<GroupNotice> page = groupNoticeRepository.findAll((Specification<GroupNotice>) (root, query, builder) -> {
			query.where(builder.equal(root.get("groupId").as(Long.class), groupId));
			return query.getRestriction();
		},pageable);
		return PageX.map(NoticeVO::of,page);
	}
}
